package com.tbit.main.pojo.maintain_performance.config;

import com.tbit.main.constant.enums.PerformanceBizType;
import com.tbit.main.pojo.PerformanceRuleGroup;
import com.tbit.main.pojo.maintain_performance.BizPerformanceConfig;
import com.tbit.main.pojo.maintain_performance.BizRuleGroup;
import com.tbit.main.pojo.maintain_performance.group.BasicPerformanceGroup;
import com.tbit.main.pojo.maintain_performance.group.RepairGroup;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 维修绩效配置的业务对象
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/24
 */
@Data
public class RepairPerformanceConfig implements BizPerformanceConfig {
    /**
     * 区域id
     */
    private Integer accountId;

    /**
     * 基础绩效
     */
    private BasicPerformanceGroup basic;

    /**
     * 维修类型绩效
     */
    private RepairGroup repair;

    public static RepairPerformanceConfig fromRaw(List<PerformanceRuleGroup> groups, Integer accountId) {
        RepairPerformanceConfig config = new RepairPerformanceConfig();
        config.setAccountId(accountId);
        for (PerformanceRuleGroup group : groups) {
            switch (group.getRuleType()) {
                case BASIC:
                    config.setBasic(new BasicPerformanceGroup());
                    config.getBasic().initFromRaw(group);
                    break;
                case REPAIR_TYPE:
                    config.setRepair(new RepairGroup());
                    config.getRepair().initFromRaw(group);
                    break;
            }
        }
        return config;
    }


    @Override
    public List<PerformanceRuleGroup> toRaw() {
        BizRuleGroup[] groups = new BizRuleGroup[]{
                basic,
                repair,
        };

        List<PerformanceRuleGroup> rawGroups = new ArrayList<>(2);
        for (BizRuleGroup group : groups) {
            if (group != null) {
                rawGroups.add(group.toRaw(PerformanceBizType.REPAIR, accountId));
            }
        }
        return rawGroups;
    }
}
